package com.example.braden.answergame.common.utils;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.braden.answergame.R;
import com.example.braden.answergame.base.BaseApplication;
import com.example.braden.answergame.common.Constant;

/**
 * Created by braden on 2018/3/8.
 */

public class ToastUtil {
    private ToastUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    private static Toast toast;
    public static boolean isShow = true;

    /**
     * 短时间显示Toast
     */
    public static void showShort(CharSequence message) {
        if (isShow) {
            if (toast == null) {
                toast = Toast.makeText(BaseApplication.getAppContext(), message,
                        Toast.LENGTH_SHORT);
            } else {
                toast.setText(message);
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.show();
        }
        //Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * 自定义Toast
     */
    public static void showCenter(Context context, String msg, int layoutId) {
        LayoutInflater inflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //自定义布局
        View view = inflater.inflate(layoutId, null);
        //自定义toast文本
        TextView mTextView = (TextView) view.findViewById(R.id.toast_msg);
        mTextView.setText(msg);
        Toast mToast = new Toast(context);
        //设置toast居中显示
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.setView(view);
    }
}
